package com.amsure.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.amsure.entity.BaseCommonEntity;
import com.amsure.service.InformationService;
import org.springframework.stereotype.Service;

import static com.amsure.entity.ConstantEntity.POSTINFORMATIONFORDATA;
import static com.amsure.entity.ConstantEntity.POSTINFORMATIONFORTOKEN;
import static com.amsure.entity.UrlEntity.URL_TOKEN;
import static com.amsure.utils.CommonUtils.getTimeNow;
import static com.amsure.utils.EncodeUtils.*;
import static com.amsure.utils.HttpUtils.DO_POST;

@Service
public class InformationServiceImpl implements InformationService {
    @Override
    public BaseCommonEntity getInformation(String information) throws Exception {
        //传入string转map
        BaseCommonEntity parseObject = JSONObject.parseObject(information, BaseCommonEntity.class);
        parseObject.setTimeStamp(getTimeNow());
        //加密 为了获取token
        BaseCommonEntity baseCommonEntity = encodeDataForToken(parseObject.getOperatorId(), parseObject.getOperatorSecret(), parseObject.getSeq(), parseObject
                .getTimeStamp(), parseObject.getDataSecret(), parseObject.getDataSecretIV(), parseObject.getSigSecret());
        //准备发送token，
        String stringReadyPostForToken = sendPostMaptoString(baseCommonEntity);
        //post
        String stringReceivePostForToken = DO_POST(URL_TOKEN, stringReadyPostForToken, null, null);
        //获取data
        String stringDecodePostForDataForToken = getPartFromAll(stringReceivePostForToken,POSTINFORMATIONFORDATA);
        //解密token
        String decodeInformationForToken = decodeInformation(stringDecodePostForDataForToken, baseCommonEntity);
        //获得token ,这边格式有问题，token是两个引号叠加
        String stringDecodeTokeTemporary = getPartFromAll(decodeInformationForToken, POSTINFORMATIONFORTOKEN);
        String stringDecodeToke = stringDecodeTokeTemporary.substring(1, stringDecodeTokeTemporary.length() - 1);
        parseObject.setAccessToken(stringDecodeToke);

        return parseObject;
    }

}
